import Mock from 'mockjs'

// 生成仪表盘统计数据
const generateDashboardStats = () => {
  return {
    totalRevenue: Mock.Random.float(1000000, 5000000, 2, 2),
    totalOrders: Mock.Random.integer(1000, 5000),
    totalCustomers: Mock.Random.integer(500, 2000),
    totalProducts: Mock.Random.integer(2000, 8000),
    monthlyGrowth: Mock.Random.float(5, 25, 2, 2),
    orderGrowth: Mock.Random.float(8, 30, 2, 2),
    customerGrowth: Mock.Random.float(3, 15, 2, 2),
    productGrowth: Mock.Random.float(10, 35, 2, 2)
  }
}

// 生成销售趋势数据
const generateSalesTrend = (days = 30) => {
  const data = []
  const now = new Date()
  
  for (let i = days - 1; i >= 0; i--) {
    const date = new Date(now)
    date.setDate(date.getDate() - i)
    
    data.push({
      date: date.toISOString().split('T')[0],
      sales: Mock.Random.float(50000, 200000, 2, 2),
      orders: Mock.Random.integer(50, 200),
      customers: Mock.Random.integer(20, 80)
    })
  }
  
  return data
}

// 生成地区销售数据
const generateRegionSales = () => {
  const regions = ['华东', '华南', '华北', '华中', '西南', '西北', '东北']
  
  return regions.map(region => ({
    region,
    sales: Mock.Random.float(100000, 800000, 2, 2),
    orders: Mock.Random.integer(100, 800),
    customers: Mock.Random.integer(50, 300)
  }))
}

// 生成产品分类数据
const generateProductCategories = () => {
  const categories = ['电子产品', '服装鞋帽', '家居用品', '食品饮料', '图书音像', '运动户外', '美妆护肤']
  
  return categories.map(category => ({
    category,
    sales: Mock.Random.float(50000, 300000, 2, 2),
    count: Mock.Random.integer(100, 800),
    percentage: Mock.Random.float(5, 25, 2, 2)
  }))
}

// 生成最近订单数据
const generateRecentOrders = (count = 10) => {
  const orders = []
  
  for (let i = 0; i < count; i++) {
    orders.push({
      id: Mock.Random.string('number', 8),
      customerName: Mock.Random.cname(),
      productName: Mock.Random.ctitle(5, 15),
      amount: Mock.Random.float(100, 5000, 2, 2),
      status: Mock.Random.pick(['pending', 'processing', 'shipped', 'delivered', 'cancelled']),
      createTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss'),
      paymentMethod: Mock.Random.pick(['支付宝', '微信支付', '银行卡', '现金'])
    })
  }
  
  return orders
}

// 生成库存预警数据
const generateStockAlerts = () => {
  const alerts = []
  
  for (let i = 0; i < 8; i++) {
    alerts.push({
      id: i + 1,
      productName: Mock.Random.ctitle(5, 15),
      currentStock: Mock.Random.integer(1, 50),
      minStock: Mock.Random.integer(20, 100),
      supplier: Mock.Random.ctitle(5, 10) + '公司',
      alertLevel: Mock.Random.pick(['low', 'critical']),
      lastUpdateTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
    })
  }
  
  return alerts
}

// 生成任务待办数据
const generateTodoTasks = () => {
  const tasks = []
  
  for (let i = 0; i < 6; i++) {
    tasks.push({
      id: i + 1,
      title: Mock.Random.ctitle(10, 25),
      description: Mock.Random.csentence(10, 30),
      priority: Mock.Random.pick(['low', 'medium', 'high', 'urgent']),
      status: Mock.Random.pick(['pending', 'in-progress', 'completed']),
      assignee: Mock.Random.cname(),
      dueDate: Mock.Random.date('yyyy-MM-dd'),
      createTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
    })
  }
  
  return tasks
}

// 获取仪表盘统计数据
Mock.mock('/api/v1/dashboard/stats', 'get', () => {
  const stats = generateDashboardStats()
  
  return {
    code: 200,
    message: '获取成功',
    data: stats
  }
})

// 获取销售趋势数据
Mock.mock('/api/v1/dashboard/sales-trend', 'get', (options) => {
  const url = new URL(options.url, 'http://localhost')
  const days = parseInt(url.searchParams.get('days')) || 30
  
  const trendData = generateSalesTrend(days)
  
  return {
    code: 200,
    message: '获取成功',
    data: trendData
  }
})

// 获取地区销售数据
Mock.mock('/api/v1/dashboard/region-sales', 'get', () => {
  const regionData = generateRegionSales()
  
  return {
    code: 200,
    message: '获取成功',
    data: regionData
  }
})

// 获取产品分类数据
Mock.mock('/api/v1/dashboard/product-categories', 'get', () => {
  const categoryData = generateProductCategories()
  
  return {
    code: 200,
    message: '获取成功',
    data: categoryData
  }
})

// 获取最近订单
Mock.mock('/api/v1/dashboard/recent-orders', 'get', (options) => {
  const url = new URL(options.url, 'http://localhost')
  const count = parseInt(url.searchParams.get('count')) || 10
  
  const orders = generateRecentOrders(count)
  
  return {
    code: 200,
    message: '获取成功',
    data: orders
  }
})

// 获取库存预警
Mock.mock('/api/v1/dashboard/stock-alerts', 'get', () => {
  const alerts = generateStockAlerts()
  
  return {
    code: 200,
    message: '获取成功',
    data: alerts
  }
})

// 获取待办任务
Mock.mock('/api/v1/dashboard/todo-tasks', 'get', () => {
  const tasks = generateTodoTasks()
  
  return {
    code: 200,
    message: '获取成功',
    data: tasks
  }
})

// 获取仪表盘概览数据
Mock.mock('/api/v1/dashboard/overview', 'get', () => {
  const stats = generateDashboardStats()
  const trendData = generateSalesTrend(7) // 最近7天
  const regionData = generateRegionSales()
  const categoryData = generateProductCategories()
  const recentOrders = generateRecentOrders(5)
  const stockAlerts = generateStockAlerts()
  const todoTasks = generateTodoTasks()
  
  return {
    code: 200,
    message: '获取成功',
    data: {
      stats,
      salesTrend: trendData,
      regionSales: regionData,
      productCategories: categoryData,
      recentOrders,
      stockAlerts,
      todoTasks
    }
  }
})

// 获取实时数据
Mock.mock('/api/v1/dashboard/realtime', 'get', () => {
  const realtimeData = {
    onlineUsers: Mock.Random.integer(50, 200),
    currentOrders: Mock.Random.integer(10, 50),
    systemStatus: 'normal',
    lastUpdateTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss'),
    notifications: [
      {
        id: 1,
        type: 'info',
        message: '系统运行正常',
        time: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
      },
      {
        id: 2,
        type: 'warning',
        message: '有3个产品库存不足',
        time: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
      },
      {
        id: 3,
        type: 'success',
        message: '今日订单量增长15%',
        time: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
      }
    ]
  }
  
  return {
    code: 200,
    message: '获取成功',
    data: realtimeData
  }
}) 